Ext.onReady(function(){ 

 Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var sm = new Ext.grid.CheckboxSelectionModel();

var cm1=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'流水号',dataIndex:'serialNo',hidden:true},
{header:'开户行',dataIndex:'orgId',width:100},
{header:'账号类型',dataIndex:'accountType',width:100},
{header:'账号',dataIndex:'accountNo',width:100}, 
{header:'开户日期',dataIndex:'accountDate',width:100},
{header:'币种',dataIndex:'currencyType',width:100},
{header:'存款余额',dataIndex:'depositBalance',width:100},
{header:'月均存款余额',dataIndex:'monthBalance',width:100}
]); 

var cm2=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'流水号',dataIndex:'serialNo',hidden:true},
{header:'贷款行',dataIndex:'orgId',width:100},
{header:'业务品种',dataIndex:'businessType',width:100},
{header:'余额(元)',dataIndex:'loanBalance',width:100},
{header:'逾期/垫款金额',dataIndex:'overdueBalance',width:100}, 
{header:'拖欠利息',dataIndex:'interestBalance',width:100}
]);

var ds1=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'customer/listDeposit.do?type=02',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'serialNo'},
{name:'orgId'}, 
{name:'accountType'},  
{name:'accountNo'},  
{name:'accountDate'},  
{name:'currencyType'},  
{name:'depositBalance'},  
{name:'monthBalance'}
]) 
}); 
ds1.load({params:{start:0,limit:10}}); 

var ds2=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'customer/listLoan.do?type=02',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'serialNo'},
{name:'orgId'}, 
{name:'businessType'}, 
{name:'loanBalance'},  
{name:'overdueBalance'},  
{name:'interestBalance'}
]) 
}); 
ds2.load({params:{start:0,limit:10}}); 



var currencyTypeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=Currency',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
currencyTypeStore.load();

var accountTypeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=AccountType',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
accountTypeStore.load();

var bankTypeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=BankType',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
bankTypeStore.load();

var addDebtTypeLevelOneStore=new Ext.data.Store({
	proxy:new Ext.data.HttpProxy({
		url:'showDebtTypeLevelOne.do',
		method:'GET'
	}),
	reader:new Ext.data.JsonReader({},[{name:'name'},{name:'code'}]),
	autoLoad:true
});

//存款gridpanel
var grid1=new Ext.grid.GridPanel({ 
id:'customerDeposit',
renderTo:'deposit_list',
stripeRows:true,//斑马线效果
title:"他行存款列表", 
height:400, 
cm:cm1,
store: ds1,
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 10,   
                store: ds1, 
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
 {text:'新增',id: 'btnAddDeposit'},
 '-',
 {text:'查看/编辑',handler:checkMyCustomer},
 '-',
 {text:'删除',handler:deleteCustomer}
 ]              
});

//贷款gridpanel
var grid2=new Ext.grid.GridPanel({ 
id:'customerLoan',
renderTo:'loan_list',
stripeRows:true,//斑马线效果
title:"我行贷款列表", 
height:400,
cm:cm2,
store: ds2,
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 10,   
                store: ds2, 
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
 {text:'新增',id: 'btnAddLoan'},
 '-',
 {text:'查看/编辑',handler:checkMyLoan},
 '-',
 {text:'删除',handler:deleteLoan}
 ]              
});


//新增存款表单
addForm = new Ext.form.FormPanel({ 
id: 'addDepositForm', 
width:550, 
height: 280, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 100, 
items: 
[ { xtype: 'fieldset', 
title: '存款信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[ 
	
{layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
	xtype: 'textfield',
	id: 'orgId', 
	name: 'orgId', 
	fieldLabel: '开户行', 
	emptyText: '必填', 
	blankText: '开户行不能为空', 
	allowBlank: false,  
	width:130  
}]},
     {  columnWidth : .5, 	      	   
	 layout : "form",
           items : [{
xtype: 'combo', 
 id : "accountType",
              store :accountTypeStore,
              fieldLabel: '账号类型',
              valueField : 'code',
              displayField : 'name',
              typeAhead : true,
              width: 130,
              mode:'remote',  
              hiddenName:'accountType',
              triggerAction : 'all',
              selectOnFocus : true
}]} 
]},


{layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
	xtype: 'textfield',
	id: 'accountNo', 
	name: 'accountNo', 
	fieldLabel: '账号', 
	emptyText: '必填', 
	blankText: '账号不能为空', 
	allowBlank: false,  
	width:130 
}]},
     {  columnWidth : .5, 	      	   
	 layout : "form",
           items : [{
	xtype: 'datefield',
	id: 'accountDate', 
	name: 'accountDate', 
	fieldLabel: '开户日期',  
	allowBlank: true,  
	width:130 ,
	format : 'Y-m-d'
}]} 
]},

{layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
			xtype: 'combo', 
 			id : "currencyType",
              store :currencyTypeStore,
              fieldLabel: '币种',
              valueField : 'code',
              displayField : 'name',
              typeAhead : true,
              width: 130,
              mode:'remote',  
              hiddenName:'currencyType',
              triggerAction : 'all',
              selectOnFocus : true
}]},
     {  columnWidth : .5, 	      	   
	 layout : "form",
           items : [{
	xtype: 'textfield',
	id: 'depositBalance', 
	name: 'depositBalance', 
	fieldLabel: '存款余额（元）',  
	allowBlank: true,  
	width:130
}]} 
]},

{ layout : "form",
           items : [{
xtype: 'textfield',
id: 'monthBalance', 
name: 'monthBalance', 
fieldLabel: '月均存款余额（元）',  
allowBlank: true,  
width:130
}]},

{ layout : "form",
           items : [{
xtype: 'hidden',
id: 'bankType', 
name: 'bankType', 
fieldLabel: '我行他行标志',  
allowBlank: true,  
value: "02",
width:130
}]},

{ layout : "form",
           items : [{
xtype: 'hidden', 
 id : "serialNo",
              fieldLabel: '贷款流水号',
              allowBlank: true
}]},
]}]});


//新增贷款表单
addLoanForm = new Ext.form.FormPanel({ 
id: 'addLoanForm', 
width: 500, 
height: 250, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 120, 
items: 
[ { xtype: 'fieldset', 
title: '贷款信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[ 
{ layout : "form",
    items : [{
xtype: 'textfield',
id: 'orgId', 
name: 'orgId', 
fieldLabel: '贷款行',  
allowBlank: true,  
width:160
}]},

{ layout : "form",
           items : [{
        	   xtype: 'combo', 
       		id : "businessType",
       		store :addDebtTypeLevelOneStore,
       		fieldLabel: '业务品种',
       		valueField : 'code',
       		displayField : 'name',
       		typeAhead : true,
       		width: 160,
       		mode:'remote', 
       		emptyText : '业务品种...',
       		editable: false,
       		triggerAction : 'all',
       		selectOnFocus : true,
       		hiddenName:'businessType',
}]},

	
{ layout : "form",
           items : [{
xtype: 'textfield',
id: 'loanBalance', 
name: 'loanBalance', 
fieldLabel: '余额（元）',  
allowBlank: true,  
width:160
}]},
	
	
{ layout : "form",
           items : [{
xtype: 'textfield',
id: 'overdueBalance', 
name: 'overdueBalance', 
fieldLabel: '逾期/垫款金额（元）',  
allowBlank: true,  
width:160
}]},

	
{ layout : "form",
           items : [{
xtype: 'textfield',
id: 'interestBalance', 
name: 'interestBalance', 
fieldLabel: '拖欠利息（元）',  
allowBlank: true,  
width:160
}]},

{ layout : "form",
           items : [{
xtype: 'hidden',
id: 'bankType', 
name: 'bankType', 
fieldLabel: '我行他行标志',  
allowBlank: true,  
value: "02",
width:130
}]},

{ layout : "form",
           items : [{
xtype: 'hidden', 
 id : "serialNo",
              fieldLabel: '贷款流水号',
              allowBlank: true
}]}
]}]});

//新增窗口 
addWin = new Ext.Window({ 
id: 'addDepositWin', 
title: '新增客户-存款信息', 
width: 550, 
height: 300, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  	
{ text: '保存', id: 'btnDepositSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addDepositWin').hide(); } }

] 
});

//新增窗口 
addLoanWin = new Ext.Window({ 
id: 'addLoanWin', 
title: '新增客户-贷款信息', 
width: 360, 
height: 250, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addLoanForm, 
buttons: 
[  
{ text: '保存', id: 'btnLoanSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addLoanWin').hide(); } }

] 
});

function addRoleFunction() {
	
	Ext.getCmp("bankType").setValue("02");
	
	var addForm = Ext.getCmp("addDepositForm");

	 addForm.form.doAction('submit', { 

                    url : 'customer/addDeposit.do?bankType=02', 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       
                       Ext.getCmp('customerDeposit').store.reload();
                       Ext.Msg.alert('提示', '保存成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '保存失败！'); 
 
                        } 
                    });  
 }
  

function addLoanFunction() {

	
	var addForm = Ext.getCmp("addLoanForm");
	Ext.getCmp("bankType").setValue("02");

	 addForm.form.doAction('submit', { 

                    url : 'customer/addLoan.do?bankType=02', 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       
                       Ext.getCmp('customerLoan').store.reload();
                       Ext.Msg.alert('提示', '保存成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '保存失败！'); 
 
                        } 
                    });  
 }


//添加按钮单击事件 
function btnAddClick() { 
Ext.getCmp('addDepositForm').form.reset(); 
Ext.getCmp("addDepositWin").buttons[0].handler = addRoleFunction; 
Ext.getCmp("addDepositWin").show(); 
}

function btnAddLoanClick() { 
Ext.getCmp('addLoanForm').form.reset(); 
Ext.getCmp("addLoanWin").buttons[0].handler = addLoanFunction; 
Ext.getCmp("addLoanWin").show(); 
}

//新增按钮 
var addUserBtn = Ext.getCmp('btnAddDeposit'); 
addUserBtn.on('click', btnAddClick); 

var addLoanBtn = Ext.getCmp('btnAddLoan');
addLoanBtn.on('click', btnAddLoanClick); 

//删除地址
function deleteCustomer(){
var grid1 = Ext.getCmp('customerDeposit'); 
if (grid1.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中删除项！"); 
} 
else{
Ext.MessageBox.confirm('提示', '确实要删除所选项吗？', deleteConfirm);
}
}

function deleteConfirm(btn){
if (btn == 'yes') {
//alert(grid1.getSelectionModel().getSelections()[0].data.serialNo);
 Ext.Ajax.request({
  url:'customer/deleteDeposit.do',
  params : { serialNo: grid1.getSelectionModel().getSelections()[0].data.serialNo},
  method:'POST',
  success:function(){
   Ext.MessageBox.alert("提示","删除成功！");
   Ext.getCmp('customerDeposit').store.reload(); 
  },failure:function(){
    Ext.MessageBox.alert("提示", "删除失败！"); 
  }
});
}
}

//删除地址
function deleteLoan(){
var grid2 = Ext.getCmp('customerLoan'); 
//alert("1");
if (grid2.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中删除项！"); 
} 
else{
Ext.MessageBox.confirm('提示', '确实要删除所选项吗？', deleteLoanConfirm);
}
}


function deleteLoanConfirm(btn){
if (btn == 'yes') {
//alert(grid2.getSelectionModel().getSelections()[0].data.serialNo);
 Ext.Ajax.request({
  url:'customer/deleteLoan.do',
  params : { serialNo: grid2.getSelectionModel().getSelections()[0].data.serialNo},
  method:'POST',
  success:function(){
   Ext.MessageBox.alert("提示","删除成功！");
   Ext.getCmp('customerLoan').store.reload(); 
  },failure:function(){
    Ext.MessageBox.alert("提示", "删除失败！"); 
  }
});
}
}

//编辑存款
function editAddressFunction(){
	
	var addForm = Ext.getCmp("addDepositForm");

	 addForm.form.doAction('submit', { 

                    url : 'customer/editDeposit.do?bankType=02&serialNo='+grid1.getSelectionModel().getSelections()[0].data.serialNo, 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       
                      Ext.getCmp('customerDeposit').store.reload();
                       Ext.Msg.alert('提示', '编辑成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '编辑失败！'); 
 
                        } 
                    }); 
}

//编辑贷款
function editLoanFunction(){
	
	var addForm = Ext.getCmp("addLoanForm");

	 addForm.form.doAction('submit', { 

                    url : 'customer/editLoan.do?bankType=02&serialNo='+grid2.getSelectionModel().getSelections()[0].data.serialNo, 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       
                      Ext.getCmp('customerLoan').store.reload();
                       Ext.Msg.alert('提示', '编辑成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '编辑失败！'); 
 
                        } 
                    }); 
}

function checkMyCustomer(){
if (grid1.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要编辑的项！"); 
}
else{
	var serialNo = grid1.getSelectionModel().getSelections()[0].data.serialNo;
	
	Ext.getCmp('addDepositForm').form.reset(); 
	Ext.getCmp("addDepositWin").buttons[0].handler = editAddressFunction; 
	//alert(serialNo);
	Ext.Ajax.request({
url:'customer/getDeposit.do',
params : { serialNo: grid1.getSelectionModel().getSelections()[0].data.serialNo},
method:'POST',
  success:function(response){
  //alert(response.responseText);
  var addJSON = Ext.util.JSON.decode(response.responseText);
  	Ext.getCmp("serialNo").setValue(addJSON.serialNo);
	Ext.getCmp("orgId").setValue(addJSON.orgId);
  	Ext.getCmp("accountType").setValue(addJSON.accountType);
  	
	Ext.getCmp("accountNo").setValue(addJSON.accountNo);
   	Ext.getCmp("accountDate").setValue(addJSON.accountDate);
	Ext.getCmp("currencyType").setValue(addJSON.currencyType);
	Ext.getCmp("depositBalance").setValue(addJSON.depositBalance);
  	Ext.getCmp("monthBalance").setValue(addJSON.monthBalance);
  	Ext.getCmp("bankType").setValue(addJSON.bankType);
  	
	Ext.getCmp("addDepositWin").show(); 
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 
}
}

function checkMyLoan(){
if (grid2.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要编辑的项！"); 
}
else{
	var serialNo = grid2.getSelectionModel().getSelections()[0].data.serialNo;
	
	Ext.getCmp('addLoanForm').form.reset(); 
	Ext.getCmp("addLoanWin").buttons[0].handler = editLoanFunction; 
//	alert(serialNo);
	Ext.Ajax.request({
url:'customer/getLoan.do',
params : { serialNo: grid2.getSelectionModel().getSelections()[0].data.serialNo},
method:'POST',
  success:function(response){
//  alert(response.responseText);
 var addJSON = Ext.util.JSON.decode(response.responseText);
  	Ext.getCmp("serialNo").setValue(addJSON.serialNo);
	Ext.getCmp("loanBalance").setValue(addJSON.loanBalance);
	Ext.getCmp("interestBalance").setValue(addJSON.interestBalance);
	Ext.getCmp("overdueBalance").setValue(addJSON.overdueBalance);
	Ext.getCmp("businessType").setValue(addJSON.businessType);
	
	Ext.getCmp("bankType").setValue(addJSON.bankType);
	//alert(Ext.getCmp("bankType"));
	//alert(Ext.getCmp("bankType").getValue());
  	
	Ext.getCmp("addLoanWin").show(); 
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 
}
}

function rowdblclickFn(grid, rowIndex, e){//双击事件  
             var row = grid.getStore().getAt(rowIndex); 
             var selectedCustId = row.get("custId");
             Ext.Ajax.request({
  url:'checkRequest.do',
  method:'GET',
  success:function(){
  window.location.href="checkMyCustomer.do?CustId="+selectedCustId;
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
});  
           //  window.location.href="view/customer/CheckCustomer_General.jsp?custId="+selectedCustId;
        }   
        grid1.addListener('rowdblclick',checkMyCustomer);
        grid2.addListener('rowdblclick',checkMyLoan);
/*grid.on('rowdblclick',function(){ window.location.href="view/customer/CheckCustomer_General.jsp" });*/
}); 